﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.WindowsAzure
Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure

Namespace Controllers

    <CustomAuthorize(Roles:=PrivilegeConstants.AdminPrivilege)> _
    Public MustInherit Class StorageItemController
        Inherits Controller
        <CLSCompliant(False)> _
        Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository)
            Me.UserPrivilegesRepository = userPrivilegesRepository
        End Sub

        Private privateUserPrivilegesRepository As IUserPrivilegesRepository
        <CLSCompliant(False)> _
        Protected Property UserPrivilegesRepository() As IUserPrivilegesRepository
            Get
                Return privateUserPrivilegesRepository
            End Get
            Private Set(ByVal value As IUserPrivilegesRepository)
                privateUserPrivilegesRepository = value
            End Set
        End Property

        Protected Shared Function GetStorageAccountFromConfigurationSetting() As CloudStorageAccount
            Dim account As CloudStorageAccount = Nothing
            Try
                account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString")
            Catch e1 As InvalidOperationException
                account = Nothing
            End Try

            Return account
        End Function

        Protected Sub RemovePrivilegeFromUser(ByVal user As String, ByVal privilege As String)
            Me.UserPrivilegesRepository.RemovePrivilegeFromUser(user, privilege)
        End Sub

        Protected Sub AddPrivilegeToUser(ByVal user As String, ByVal privilege As String)
            Me.UserPrivilegesRepository.AddPrivilegeToUser(user, privilege)
        End Sub

        Protected Sub SetPublicPrivilege(ByVal privilege As String, ByVal isPublic As Boolean)
            If isPublic Then
                Me.UserPrivilegesRepository.AddPublicPrivilege(privilege)
            Else
                Me.UserPrivilegesRepository.DeletePublicPrivilege(privilege)
            End If
        End Sub
    End Class
End Namespace